package com.example.demo.service.impl;

import com.example.demo.dataobject.User;
import com.example.demo.repository.UserRespository;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.NoSuchElementException;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRespository user;

    @Override
    public boolean login(String username, String password) {

        try {
            User u = user.findByUsernameAndPassword(username, password);


            if (u.getPassword().equals(password))
                return true;

            return false;
        }catch (NullPointerException e){
            return false;
        }
    }

    @Override
    public boolean regist(String username, String password) {
        User select = null;
        try{
            select = user.findByUsername(username);
        }catch (NullPointerException e){

        }

        if(select != null){
            return false;
        }

        User u = new User();
        try{
            u.setUsername(username);
            u.setPassword(password);

            user.save(u);
            return true;
        }
        catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }

    }
}
